
clear all

cd "$root"
use "Data/data_ps.dta"
cd "$output"

global FE "t_block"

* Create weighting variables used in secondary specifications *
cap drop wgt_ps
gen wgt_ps = 1/ps

*** Descriptive statistics *****************************************************
********************************************************************************

estpost summarize PS_vreg PS_n_stations PS_new PS_vreg_11 PS_n_stations_11 PS_turnout_11 PS_invalid_11 PS_covered PS_urban if seq==1, listwise
est store A

esttab A using "Table A1.tex", append ///
	nonum cells("mean(fmt(2) label($ $)) sd(fmt(2) label($ $)) min(fmt(2) label($ $)) max(fmt(2) label($ $))") ///
	label nomtitles refcat(PS_vreg "\textbf{C. Polling station-level variables} (\textit{n} = 5386)", nolabel) b(3) se f nogaps booktabs sfmt(3 0) noobs compress

label variable voteshare "\hspace{0.1cm} Vote share"
label variable perf_pred "\hspace{0.1cm} Measure of debate performance"
label variable pref_match_share "\hspace{0.1cm} Measure of policy alignment"
	
estpost summarize voteshare perf_pred pref_match_share if incumbent==1, listwise
est store B
estpost summarize voteshare perf_pred pref_match_share if top_chal==1, listwise
est store C

esttab B C using "Table A3.tex", append ///
	nonum cells("mean(fmt(2) label($ $)) sd(fmt(2) label($ $)) min(fmt(2) label($ $)) max(fmt(2) label($ $))") ///
	label nomtitles substitute(_ _) refcat(voteshare "\textbf{Table 6 (PS-level)}", nolabel) b(3) se f nogaps booktabs sfmt(3 0) noobs compress

	
*** Balance tables  ************************************************************
********************************************************************************

foreach var in ED_week ED_n_cand_17 ED_incum_run ED_log_reg_17 ED_n_debates /// 
ED_vote_1_11 ED_vote_2_11 ED_vote_3_11 ED_hhi_11 ED_turnout_11 ED_ran_11 ED_pop_dens_08 /// 
ED_gsm_15 ED_own_radio ED_radio_news ED_age_18 ED_ed_sec ED_radio_cov /// 
PS_n_stations PS_new PS_n_stations_11 PS_vreg_11 PS_turnout_11 PS_invalid_11 PS_vreg PS_covered PS_urban {
	
	cap qui eststo `var': areg `var' i.t_invite if seq==1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	cap qui eststo `var'_w1: areg `var' i.t_invite if seq==1 [aw=wgt_ps], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/PS"
		qui sum `var' [aw=wgt_ps] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	cap qui eststo `var'_w2: areg `var' i.t_invite if seq==1  [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg"
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
		
}

*** Polling station-level balance (district covariates) ***

local lines "\cmidrule(lr{1em}){2-4} \cmidrule(lr{1em}){5-7} \cmidrule(lr{1em}){8-10} "	
local titles "& \multicolumn{3}{c}{ \textbf{VS of 1st place (2011)} } & \multicolumn{3}{c}{ \textbf{VS of 1st place (2011)} } & \multicolumn{3}{c}{ \textbf{VS of 3rd place (2011)} } \\"
esttab ED_vote_1_11 ED_vote_1_11_w1 ED_vote_1_11_w2 ED_vote_2_11 ED_vote_2_11_w1 ED_vote_2_11_w2 ED_vote_3_11 ED_vote_3_11_w1 ED_vote_3_11_w2 using "Table A8.tex", ///
	replace nonumber nomtitles posthead("`titles'" "`lines'") nonumber b(3) se label alignment(S S S) f nomtitles nogaps booktabs  scalars("Control_Mean \hspace{0.1cm} Control Mean") noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels drop(_cons)
local titles "\midrule & \multicolumn{3}{c}{ \textbf{Turnout (2011)} } & \multicolumn{3}{c}{ \textbf{Share ran in 2011} } & \multicolumn{3}{c}{ \textbf{VS HHI (2011)} } \\"	
esttab ED_turnout_11 ED_turnout_11_w1 ED_turnout_11_w2 ED_ran_11 ED_ran_11_w1 ED_ran_11_w2 ED_hhi_11 ED_hhi_11_w1 ED_hhi_11_w2  using "Table A8.tex", ///
	append nonumber nomtitles posthead("`titles'" "`lines'") b(3) se label alignment(S S S) f nomtitles nogaps booktabs  scalars("Control_Mean \hspace{0.1cm} Control Mean") noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels drop(_cons)
local titles "\midrule & \multicolumn{3}{c}{ \textbf{N. candidates (2017)} } & \multicolumn{3}{c}{ \textbf{Incumbent ran (2017)} } & \multicolumn{3}{c}{ \textbf{Number of debates} } \\"		
esttab ED_n_cand_17 ED_n_cand_17_w1 ED_n_cand_17_w2 ED_incum_run ED_incum_run_w1 ED_incum_run_w2 ED_n_debates ED_n_debates_w1 ED_n_debates_w2 using "Table A8.tex", ///
	append nonumber nomtitles posthead("`titles'" "`lines'") b(3) se label alignment(S S S) f nomtitles nogaps booktabs  scalars("Control_Mean \hspace{0.1cm} Control Mean") noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels drop(_cons)
local titles "\midrule & \multicolumn{3}{c}{ \textbf{Log pop. dens. (2008)} } & \multicolumn{3}{c}{ \textbf{Log reg. voters (2017)} } & \multicolumn{3}{c}{ \textbf{GSM coverage (2016)} } \\"			
esttab ED_pop_dens_08 ED_pop_dens_08_w1 ED_pop_dens_08_w2  ED_log_reg_17 ED_log_reg_17_w1 ED_log_reg_17_w2 ED_gsm_15 ED_gsm_15_w1 ED_gsm_15_w2 using "Table A8.tex", ///
	append nonumber nomtitles posthead("`titles'" "`lines'") b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean") noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels drop(_cons)
local titles "\midrule & \multicolumn{3}{c}{ \textbf{Share owns radio (2015)} } & \multicolumn{3}{c}{ \textbf{Share radio news (2015)} } & \multicolumn{3}{c}{ \textbf{Radio coverage (2016)} } \\"				
esttab ED_own_radio ED_own_radio_w1 ED_own_radio_w2  ED_radio_news ED_radio_news_w1 ED_radio_news_w2 ED_radio_cov ED_radio_cov_w1 ED_radio_cov_w2 using "Table A8.tex", ///
	append nonumber nomtitles posthead("`titles'" "`lines'") b(3) se label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean") noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels drop(_cons)
local titles "\midrule & \multicolumn{3}{c}{ \textbf{Debate week} } & \multicolumn{3}{c}{ \textbf{Share sec. ed. (2008)} } & \multicolumn{3}{c}{ \textbf{Share 18+ (2008)} } \\"				
esttab ED_week ED_week_w1 ED_week_w2  ED_ed_sec ED_ed_sec_w1 ED_ed_sec_w2 ED_age_18 ED_age_18_w1 ED_age_18_w2 using "Table A8.tex", ///
	append nonumber nomtitles posthead("`titles'" "`lines'") b(3) se label alignment(S S S) f nomtitles nogaps booktabs  sfmt(3 %9.0fc 0) scalars("Control_Mean \hspace{0.1cm} Control Mean"  "N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels drop(_cons)

*** Polling station-level balance (polling station covariates) ***
	
local lines "\cmidrule(lr{1em}){2-4} \cmidrule(lr{1em}){5-7} \cmidrule(lr{1em}){8-10}"	
local titles " & \multicolumn{3}{c}{ \textbf{N. PS in VRC (2017)} } & \multicolumn{3}{c}{ \textbf{N. PS in VRC (2011)} } & \multicolumn{3}{c}{ \textbf{New VRC} } \\"
esttab PS_n_stations PS_n_stations_w1 PS_n_stations_w2 PS_n_stations_11 PS_n_stations_11_w1 PS_n_stations_11_w2 PS_new PS_new_w1 PS_new_w2 using "Table A9.tex", ///
	replace nonumber nomtitles posthead("`titles'" "`lines'") nonumber b(3) se label alignment(S S S) f nomtitles nogaps booktabs  scalars("Control_Mean \hspace{0.1cm} Control Mean") noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels drop(_cons)
local titles "\midrule & \multicolumn{3}{c}{ \textbf{Urban} } & \multicolumn{3}{c}{ \textbf{Reg. Voters (2017)} } & \multicolumn{3}{c}{ \textbf{Reg. Voters (2011)} } \\"
esttab PS_urban PS_urban_w1 PS_urban_w2 PS_vreg PS_vreg_w1 PS_vreg_w2 PS_vreg_11 PS_vreg_11_w1 PS_vreg_11_w2 using "Table A9.tex", ///
	append nonumber nomtitles posthead("`titles'" "`lines'") nonumber b(3) se label alignment(S S S) f nomtitles nogaps booktabs  scalars("Control_Mean \hspace{0.1cm} Control Mean") noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels drop(_cons)
local titles "\midrule & \multicolumn{3}{c}{ \textbf{Turnout (2011)} } & \multicolumn{3}{c}{ \textbf{Invalid votes (2011)} } & \multicolumn{3}{c}{ \textbf{Radio covered} } \\"
esttab PS_turnout_11 PS_turnout_11_w1 PS_turnout_11_w2  PS_invalid_11 PS_invalid_11_w1 PS_invalid_11_w2 PS_covered PS_covered_w1 PS_covered_w2 using "Table A9.tex", ///
	append nonumber nomtitles posthead("`titles'" "`lines'") nonumber b(3) se label sfmt(3 %9.0fc 0) alignment(S S S) f nomtitles nogaps booktabs  scalars("Control_Mean \hspace{0.1cm} Control Mean"  "N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels drop(_cons)
	
*** Effects on voting outcomes *************************************************
********************************************************************************

*** NOTE: This only produces the bottom panel of Table 6. Run make_tables_ps.do to produce the top panel ***

global ctrls "std_match std_perf party_UP c.ED_* c.PS_*" 

foreach var in voteshare {

	capture drop std_perf std_match
	egen std_perf=std(perf_pred) if incumbent==1
	egen std_match=std(pref_match_share) if incumbent==1

	eststo i_`var': capture quietly areg `var' i.t_invite $ctrls if incumbent==1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo i_`var'_w1: capture quietly areg `var' i.t_invite $ctrls if incumbent==1 [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg"	
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo i_`var'_w2: capture quietly areg `var' i.t_invite $ctrls if incumbent==1 [aw=wgt_ps], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/PS"	
		qui sum `var' [aw=wgt_ps] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
		
	eststo i_`var'_i: capture quietly areg `var' i.t_invite##c.std_perf $ctrls if incumbent==1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo i_`var'_i_w1: capture quietly areg `var' i.t_invite##c.std_perf $ctrls if incumbent==1 [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg"	
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo i_`var'_i_w2: capture quietly areg `var' i.t_invite##c.std_perf $ctrls if incumbent==1 [aw=wgt_ps], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/PS"	
		qui sum `var' [aw=wgt_ps] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
		
	eststo i_`var'_i1: capture quietly areg `var' i.t_invite##c.std_match $ctrls if incumbent==1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo i_`var'_i1_w1: capture quietly areg `var' i.t_invite##c.std_match $ctrls if incumbent==1 [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg"	
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo i_`var'_i1_w2: capture quietly areg `var' i.t_invite##c.std_match $ctrls if incumbent==1 [aw=wgt_ps], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/PS"	
		qui sum `var' [aw=wgt_ps] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
		
	capture drop std_perf std_match
	egen std_perf=std(perf_pred) if top_chal==1
	label variable std_perf "\hspace{0.1cm} $\widehat{\text{Performance}}$"
	egen std_match=std(pref_match_share) if top_chal==1
	label variable std_match "\hspace{0.1cm} Policy alignment"	
		
	eststo c_`var': capture quietly areg `var' i.t_invite $ctrls if top_chal==1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo c_`var'_w1: capture quietly areg `var' i.t_invite $ctrls if top_chal==1 [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg"	
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo c_`var'_w2: capture quietly areg `var' i.t_invite $ctrls if top_chal==1 [aw=wgt_ps], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/PS"	
		qui sum `var' [aw=wgt_ps] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	
	eststo c_`var'_i: capture quietly areg `var' i.t_invite##c.std_perf $ctrls if top_chal==1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo c_`var'_i_w1: capture quietly areg `var' i.t_invite##c.std_perf $ctrls if top_chal==1 [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg"	
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo c_`var'_i_w2: capture quietly areg `var' i.t_invite##c.std_perf $ctrls if top_chal==1 [aw=wgt_ps], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/PS"	
		qui sum `var' [aw=wgt_ps] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
		
	eststo c_`var'_i1: capture quietly areg `var' i.t_invite##c.std_match $ctrls if top_chal==1, absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "No"
		qui sum `var' if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo c_`var'_i1_w1: capture quietly areg `var' i.t_invite##c.std_match $ctrls if top_chal==1 [aw=wgt_reg], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "Reg"	
		qui sum `var' [aw=wgt_reg] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
	eststo c_`var'_i1_w2: capture quietly areg `var' i.t_invite##c.std_match $ctrls if top_chal==1 [aw=wgt_ps], absorb( $FE ) cluster(EDCODE)
		qui estadd local Weight "1/PS"	
		qui sum `var' [aw=wgt_ps] if e(sample)==1 & t_invite==0
		qui estadd scalar Control_Mean = `r(mean)'
		
}

*** Effects on voting outcomes (Panel B) ***
local numbers "\\ \textbf{2. Polling station-level}  & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) \\ \midrule"
esttab i_voteshare i_voteshare_w2 i_voteshare_w1 i_voteshare_i i_voteshare_i_w2 i_voteshare_i_w1 i_voteshare_i1 i_voteshare_i1_w2 i_voteshare_i1_w1 using "Table 6.tex", ///
	append nonumber posthead("`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) interaction(" $\times$ ") b(3) se ///
	label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean" "N \hspace{0.1cm} Observations") sfmt(3 %9.0fc 0) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep(1.t_invit*)
esttab c_voteshare c_voteshare_w2 c_voteshare_w1 c_voteshare_i c_voteshare_i_w2 c_voteshare_i_w1 c_voteshare_i1 c_voteshare_i1_w2 c_voteshare_i1_w1 using "Table 6.tex", ///
	append nonumber refcat(1.t_invite "\textbf{B. Challengers}", nolabel) interaction(" $\times$ ") b(3) se label alignment(S S S) f nomtitles nogaps booktabs ///
	scalars("Control_Mean \hspace{0.1cm} Control Mean" "N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(3 %9.0fc 0) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep(1.t_invit*)

*** Effects on voting outcomes (Panel B) (with control coefficients) ***
global keep_vars "1.t_invite party_UP PS_* ED_*"
local super_title " & & & & \multicolumn{12}{c}{ \textbf{Interaction term:} } \\"
local titles "& \multicolumn{6}{c}{ \textbf{Main effect} } & \multicolumn{6}{c}{ \textbf{Std. Performance} }  & \multicolumn{6}{c}{ \textbf{Std. Policy alignment} } \\"
local lines "\cmidrule(lr{0.5em}){2-7} \cmidrule(lr{0.5em}){8-13} \cmidrule(lr{0.5em}){14-19}"
local numbers "\textbf{2. Polling station-level} & \multicolumn{2}{c}{(1)} & \multicolumn{2}{c}{(2)} & \multicolumn{2}{c}{(3)} & \multicolumn{2}{c}{(4)} & \multicolumn{2}{c}{(5)} & \multicolumn{2}{c}{(6)} & \multicolumn{2}{c}{(7)} & \multicolumn{2}{c}{(8)} & \multicolumn{2}{c}{(9)} \\ \midrule"

esttab i_voteshare i_voteshare_w2 i_voteshare_w1 i_voteshare_i i_voteshare_i_w2 i_voteshare_i_w1 i_voteshare_i1 i_voteshare_i1_w2 i_voteshare_i1_w1 using "Table A29.tex", ///
	replace nonumber wide posthead("`super_title'" "`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{A. Incumbent}", nolabel) interaction(" $\times$ ") b(3) se ///
	label alignment(S S S) f nomtitles nogaps booktabs scalars("Control_Mean \hspace{0.1cm} Control Mean" "N \hspace{0.1cm} Observations") sfmt(3 %9.0fc 0) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep( $keep_vars ) order(1.t_invit*)
esttab c_voteshare c_voteshare_w2 c_voteshare_w1 c_voteshare_i c_voteshare_i_w2 c_voteshare_i_w1 c_voteshare_i1 c_voteshare_i1_w2 c_voteshare_i1_w1 using "Table A30.tex", ///
	replace nonumber wide posthead("`super_title'" "`titles'" "`lines'" "`numbers'") refcat(1.t_invite "\textbf{B. Challengers}", nolabel) interaction(" $\times$ ") b(3) se label alignment(S S S) f nomtitles nogaps booktabs ///
	scalars("Control_Mean \hspace{0.1cm} Control Mean" "N \hspace{0.1cm} Observations" "Weight \hspace{0.1cm} Weight") sfmt(3 %9.0fc 0) noobs compress star(* 0.10 ** 0.05 *** 0.01) nobaselevels keep( $keep_vars ) order(1.t_invit*)
